package cn.ljn.system.service.impl;

import cn.ljn.system.domain.Role;
import cn.ljn.system.mapper.MenuMapper;
import cn.ljn.system.mapper.PermissionMapper;
import cn.ljn.system.service.IRoleService;
import cn.ljn.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ljn
 * @since 2023-03-09
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void add(Role role) {
        super.add(role);

        // 添加角色和菜单中间表
        menuMapper.addMiddle(role.getId(),role.getMenus());
        // 添加角色和按钮中间表
        permissionMapper.addMiddle(role.getId(),role.getPermissions());
    }

    @Override
    public void update(Role role) {
        super.update(role);
        // 删除中间表
        menuMapper.deleteByRoleId(role.getId());
        // 添加中间表
        menuMapper.addMiddle(role.getId(),role.getMenus());

        // 删除中间表
        permissionMapper.deleteByRoleId(role.getId());
        // 添加中间表
        permissionMapper.addMiddle(role.getId(),role.getPermissions());
    }
}
